<?php if (!defined("BASEPATH")) exit("No direct script access allowed"); ?>

<script type="text/javascript">
	Ext.onReady(function() {

		var accStore = new Ext.data.JsonStore({
			totalProperty: 'total', 
			root: 'results', 
			sortInfo: {field: 'accName', direction: 'ASC'},
			remoteSort: true,
			proxy : new Ext.data.HttpProxy({
				url: '<?=site_url()?>admin/get_accomodation/',
				method: 'GET'
			}),
			fields: [
				{name: 'accId'},
				{name: 'accName'},
				{name: 'accUrlName'},
				{name: 'insList'},
				{name: 'insNameList'},
				{name: 'accCityId'},
				{name: 'cityName'},
				{name: 'accLatitude'},
				{name: 'accLongitude'},
				{name: 'e2cTempFlag'},
				{name: 'accPresenter'}
			]
		});

		var accGrid = new Ext.grid.GridPanel({
			id: 'accGrid',
			store: accStore,
			viewConfig:{
				emptyText: '<?=$this->db->escape_str($this->lang->line("THERE_IS_CURRENTLY_NO_DATA_TO_DISPLAY"))?>',
				getRowClass: function(record, index) {
					if(record.data.e2cTempFlag == 'I') return 'temp_data_exists';
					else if(record.data.accPresenter == 'I') return 'presenter_live';
					else return '';
				}
			},
			columns: [
				{id: 'accName', header: '<?=$this->db->escape_str($this->lang->line("ACCOMODATION"))?>', width: 200, sortable: true, dataIndex: 'accName'},
				{id: 'insNameList', header: '<?=$this->db->escape_str($this->lang->line("INSTITUTE"))?>', width: 200, sortable: false, dataIndex: 'insNameList'},
				{header: '<?=$this->db->escape_str($this->lang->line("CITY"))?>', width: 150, sortable: true, dataIndex: 'cityName'}
			],
			stripeRows: true,
			autoExpandColumn: 'insNameList',
			autoHeight: true,
			autoWidth: true,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),

			listeners: {

				rowdblclick: function(grid, rowIndex, event) {
					if(rowIndex != 'new') {
						var record = grid.getStore().getAt(rowIndex);
					} else {
						var record = new Object();
						record.data = new Object();
					}
					DataEdit.showWindow('ACC', ((rowIndex != 'new')?(''):('-')) + record.data.accId)
				}

			},
			border: false,
			frame: false,

			tbar: {
				items: [
					{
						icon: '<?=MEDIA?>icon/application_form_add.png',
						text: '<?=$this->db->escape_str($this->lang->line("ADD_NEW"))?>',
						handler: function(){
							DataEdit.showWindow('ACC', -1);
						}
					}, {
							xtype: 'tbseparator'
					},
					{
						icon: '<?=MEDIA?>icon/application_form_edit.png',
						text: '<?=$this->db->escape_str($this->lang->line("EDIT"))?>',
						ref: '../editBtn',
						disabled: true,
						handler: function() {
							Ext.getCmp('accGrid').fireEvent('rowdblclick', Ext.getCmp('accGrid'), Ext.getCmp('accGrid').store.indexOf(Ext.getCmp('accGrid').getSelectionModel().getSelected()), new Object());
						}
					}, {
							xtype: 'tbseparator'
					},
					{
						icon: '<?=MEDIA?>icon/application_form_delete.png',
						text: '<?=$this->db->escape_str($this->lang->line("DELETE"))?>',
						ref: '../removeBtn',
						disabled: true,
						handler: function() {
							DataEdit.delete('ACC', Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accId, Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accName);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_preview.png',
						text: '<?=$this->db->escape_str($this->lang->line("PREVIEW"))?>',
						ref: '../previewBtn',
						disabled: true,
						handler: function(){
							window.open('<?=site_url()?>preview/accomodations/' + Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accUrlName);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish.png',
						text: '<?=$this->db->escape_str($this->lang->line("PUBLICATION"))?>',
						ref: '../publishBtn',
						disabled: true,
						handler: function(){
							DataEdit.publish('ACC', Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accId, Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accName, 1);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish_delete.png',
						text: '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_UNPUBLISHED_DATA"))?>',
						ref: '../publishBtnDelete',
						disabled: true,
						handler: function(){
							DataEdit.publish('ACC', Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accId, Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accName, 0);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_publish_remove.png',
						text: '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_PUBLISHED_DATA"))?>',
						ref: '../unpublishBtn',
						disabled: true,
						handler: function(){
							DataEdit.unpublish('ACC', Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accId, Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accName, 2);
						}
					}, {
						xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/image.png',
						text: '<?=$this->db->escape_str($this->lang->line("MANAGING_IMAGES"))?>',
						ref: '../imageBtn',
						disabled: true,
						handler: function(){
							DataEdit.image('ACC', Ext.getCmp('accGrid').getSelectionModel().getSelections()[0].data.accId);
						}
					}
				]
			},

			bbar: 
				new Ext.PagingToolbar({
					pageSize: <?=$this->config->item("adminGridPageSize")?>,
					store: accStore,
					displayInfo: true,
					plugacc: new Ext.ux.ProgressBarPager()
				}),

			plugacc:[
				new Ext.ux.grid.Search({
					minChars:2,
					iconCls: 'icon-zoom',
					searchText:'<?=$this->db->escape_str($this->lang->line("SEARCH"))?>',
					position:'top',
					minCharsTipText:'<?=$this->db->escape_str($this->lang->line("MINIMUM_TWO_CHARACTERS_IS_REQUIRED"))?>',
					selectAllText: '<?=$this->db->escape_str($this->lang->line("SELECT_ALL"))?>',
					width:150,
					disableIndexes:[],
					autoFocus:true
				})]

		});

		Ext.getCmp('accGrid').render('grid-table-acc');

		Ext.getCmp('accGrid').getSelectionModel().on('selectionchange', function(sm){
			Ext.getCmp('accGrid').removeBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('accGrid').editBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('accGrid').publishBtn.setDisabled(!(sm.getCount() && sm.getSelected().data.e2cTempFlag == 'I'));
			Ext.getCmp('accGrid').publishBtnDelete.setDisabled(!(sm.getCount() && sm.getSelected().data.e2cTempFlag == 'I'));
			Ext.getCmp('accGrid').unpublishBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('accGrid').previewBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('accGrid').imageBtn.setDisabled(sm.getCount() < 1);
		});

		accStore.load({params:{start:0, limit:<?=$this->config->item("adminGridPageSize")?>}});


	})
</script>


<div id="grid-table-acc"></div>